package by.bsu.touragency.tag;

import java.io.IOException;
import java.util.Scanner;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.log4j.Logger;

import by.bsu.touragency.command.guest.ConfigurationManager;
import by.bsu.touragency.db.beans.BookedTour;
import by.bsu.touragency.db.beans.Person;
import by.bsu.touragency.db.beans.Tour;
import by.bsu.touragency.db.dao.BookedTourDAO;
import by.bsu.touragency.db.dao.PersonDAO;
import by.bsu.touragency.db.dao.TourDAO;
import by.bsu.touragency.notification.EmailSender;


public class BuyTag extends BodyTagSupport{

	private static final long serialVersionUID = 1L;
	private static Logger logger=Logger.getLogger("log");
	
	private TourDAO t = new TourDAO();
	private int num;
	private Person client;
	private String login;
	private boolean flag;
	private EmailSender sending;
	private Tour tour;

	public void setNum(String num) {
		this.num = new Integer(num);
	}
	public void setUser(String user) {
		this.login = new String(user);
	}
	public int doStartTag() throws JspTagException {
		flag=true;
		num = 1;
		Scanner sc = new Scanner(pageContext.getRequest().getParameter("idTour"));
		num = sc.nextInt();
		
		PersonDAO cd = new  PersonDAO();
		client = cd.readByLogin(login);
		
		TourDAO td = new TourDAO();
		tour = new Tour();
		tour = td.readNB().get(num);
		BookedTour bt = new BookedTour(tour,client);
		BookedTourDAO bd = new BookedTourDAO();
		if(bd.isExist(bt)){
			try {
				pageContext.getOut().write("TOUR IS EXIST");
			} catch (IOException e) {
				logger.debug("Tour is exist");
			}
			return SKIP_BODY;
		}
		else{
			bd.create(bt);
		}
		return EVAL_BODY_INCLUDE;
	}

	public int doAfterBody() throws JspTagException {
		logger.info("Buy tag After body");
		if (flag) {
			try {
				pageContext.getOut().write("<div class=");
				pageContext.getOut().write("\"post\"");
				pageContext.getOut().write(">");
				pageContext.getOut().write("<h1 class=");
				pageContext.getOut().write("\"title\"");
				pageContext.getOut().write(">");
				
				pageContext.getOut().write(login);
				
				pageContext.getOut().write("</h1>");
				pageContext.getOut().write("<div class=");
				pageContext.getOut().write("\"entry\"");
				pageContext.getOut().write(">");
				pageContext.getOut().write("<p><img src=\"");
				pageContext.getOut().write(pageContext.getServletContext().getContextPath()+
						ConfigurationManager.getInstance().getProperty(
						ConfigurationManager.IMAGE_PAGE_PATH)+"\"");
				pageContext.getOut().write("alt=\"\" width=");
				pageContext.getOut().write("\"140\"");
				pageContext.getOut().write("height=");
				pageContext.getOut().write("\"125\"");
				pageContext.getOut().write("class=");
				pageContext.getOut().write("\"left\"");
				pageContext.getOut().write("/>");
				
				pageContext.getOut().write("Thank you for buying at TourAgency!");
				
				pageContext.getOut().write("</p>");
				pageContext.getOut().write("<p>&nbsp;</p>");
				pageContext.getOut().write("</div>");
				pageContext.getOut().write("</div>");
				
			    flag=false;
				
				String body = "Thank you for buying at TourAgency!";
				body+="\n"+ tour.getHotel().getName()+" "+tour.getHotel().getStar().getName();
				body+="\n"+tour.getHotel().getCity().getCityName()+", "+tour.getHotel().getCity().getCountry().getCountryName();
				body+="\n"+tour.getPrice()+"$";
		        setSending(new EmailSender(client.getEmail(),client.getLogin(), body));
			} catch (IOException e) {
				logger.error("Error in buy_tag");
			}
			return EVAL_BODY_AGAIN;
		} else {
			return SKIP_BODY;
		}
	}

	public int doEndTag() throws JspTagException {
		return SKIP_BODY;
	}
	public void setSending(EmailSender sending) {
		this.sending = sending;
	}
	public EmailSender getSending() {
		return sending;
	}
	public void setT(TourDAO t) {
		this.t = t;
	}
	public TourDAO getT() {
		return t;
	}
}
